<template>
	<view>
		<view class="page">
			<view class="upload-scan">
				<scan-bar id="scanInput" ref="scanBarRef" placeholder="扫描包装条码" @code-scanned="getScanResult" :key="datekey"></scan-bar>
			</view>
			<view class="area-header">
				<u-text text="待分包数量">
				</u-text>
				<!-- <u-text :text="'已扫'+' ' + scanList.length" align="right"> -->
				<u-text :text="'已扫'+' (' + scannedList.length+')'" align="right">
				</u-text>
			</view>
			<view class="list-content">
				<!-- 列表 -->
				<u-list class="list" v-if="scannedList && scannedList.length > 0" :height="ListViewHeight">
					<u-list-item v-for="(item, index) in scannedList" :key="item.id"
						@click="scannedDelete(item, index)">
						<view class="card">
							<view class="right">
								<view class="title" style="word-break: break-all;">
									外部编号: {{item.extenalCode}}
								</view>
								<view class="body">
									物料:{{ item.materialCode }}({{ item.materialName }})
								</view>
								<view class="body">
									<label class="span">仓库号:{{ item?.warehouseCode || '待定'}}</label>
									<label class="span">存储类型:{{item?.storageTypeName || '待定'}}</label>
								</view>
								<view class="body">
									<label class="span">批次: {{item?.stockBatch || '待定'}}</label>
									<label class="span">数量: {{item?.totalQuantity || '待定'}}</label>
								</view>
								<view class="body">
									<label class="span">货位: {{item?.binCode || '待定'}}</label>
								</view>
							</view>
							<view class="left">
								<u-icon name="checkmark-circle-fill" color="#42b20e" size="26px"></u-icon>
							</view>
						</view>
						<u-line></u-line>
					</u-list-item>
				</u-list>
				<!-- 空数据 -->
				<view v-else>
					<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
				</view>
			</view>
			<u-line></u-line>
			<view class="search">
				<u-button type="primary" @click="confirmReceiveData" class="custom-style">下一步</u-button>
			</view>
		</view>
		<u-modal :show="messageShow" :closeOnClickOverlay="false" title="提示" content="确定对待分包数据进行分包提交吗？"
			:showCancelButton="true"  @confirm="messageConfirm" @cancel="messageCancel"></u-modal>
		<u-modal :show="showImport" ref="uModal" :content="`当前外部编号为${selectOrder?.extenalCode ?? ''}, 请确认是否取消分包？`"
			confirmText="确认" showCancelButton @cancel="cancelImport" @confirm="confirmImport"
			style="word-break: break-all;"></u-modal>
		<u-modal :show="isReplace" ref="uModal" :content="`请确认是否替换外部编码为${scannedList[0]?.extenalCode ?? ''}的数据？`"
			confirmText="确认" showCancelButton @cancel="replaceImport" @confirm="confirmReplacementImport"
			style="word-break: break-all;"></u-modal>
	</view>
</template>

<script setup lang="ts">
	import { computed, ref } from 'vue';
	import ScanBar from '@/components/scan-bar/index.vue';
	import { onShow, onPullDownRefresh } from '@dcloudio/uni-app';
	import { http } from "../../../utils/http";
	import { PageResult } from '../../../types/type';

	// 重置表单信息 & 光标自动blur
	const scanBarRef = ref();
	const ListViewHeight = ref(0);
	const messageShow = ref(false);

	const current = ref(1); // 当前页
	const totalPage = ref(0); //累计分页
	const total = ref(0); // 累计条数
	//已扫描集合
	const scanList = ref([]);
	const scannedList = ref([])
	onShow(async () => {
		ListViewHeight.value = uni.getSystemInfoSync().windowHeight - 160;
		console.log('ListViewHeight.value', ListViewHeight.value);
	})
	onPullDownRefresh(() => {
		current.value = 0;
		scannedList.value = [];
	});
	// 选择订单
	const selectOrder = ref({});
	const selectIndex = ref();
	const showImport = ref(false);
	const datekey = ref(new Date());
	function scannedDelete(item : any, index : number) {
		showImport.value = true;
		selectOrder.value = item;
		selectIndex.value = index;
	}
	// 取消选择订单
	function cancelImport() {
		showImport.value = false;
		selectOrder.value = {};
		// selectIndex.value = null;
	}
	// 删除
	async function confirmImport() {
		scannedList.value.splice(selectIndex.value, 1)
		showImport.value = false;
		uni.showToast({
			title: '取消成功'
		});
	}
	// 获取扫码结果
	async function getScanResult(result) {
		if (!result) {
			return
		}
		console.log('扫码结果', result);
		// current.value += 1;
		const res : PageResult<any> = await http({
			method: 'POST',
			url: '/wms/storageunit/queryByPage',
			data: {
				current: current.value,
				size: 20,
				data: {
					extenalCode: result,
				}
			}
		});
		if (res.data.length > 0) {
			console.log(res.data);
			const list = res.data
			scanBarRef.value.reset();
			if (scannedList.value.length > 0) {
				let index = scannedList.value.findIndex(item => item.extenalCode === result);
				console.log(index);
				if (index == -1) {
					//替换提示
					replaceData(list[0])
				} else {
					uni.$u.toast('这个包装已经被扫描过！');
					scanBarRef.value.reset();
				}
			} else {
				scannedList.value[0] = list[0];
				scanBarRef.value.reset();
			}
			total.value = res.total;
			totalPage.value = res.page;
			if(list[0].isCheck==1)
			{
			uni.$u.toast('这个包装还未质检！');
			scannedList.value = [];
			}
		} else {
			uni.$u.toast('未找到对应包装！');
			scanBarRef.value.reset();
		}
    scanBarRef.value.reset();
	datekey.value= new Date();
	}
	//分包提交弹出
	async function confirmReceiveData() {
		if (scannedList.value.length) {
			messageShow.value = true;
		} else {
			uni.$u.toast('待分包数量不能为空！');
		}
	}
	//分包提交确认
	async function messageConfirm() {
		messageShow.value = false;
		uni.navigateTo({
			url: `/pages/use/unpack/detail?result=${JSON.stringify(scannedList.value[0])}`
		});
	}
	//提交取消
	function messageCancel() {
		messageShow.value = false;
	}
	const isReplace = ref(false)
	const replace = ref()
	//替换
	function replaceData(item : any) {
		isReplace.value = true
		replace.value = item
	}

	function replaceImport() {
		isReplace.value = false;
		scanBarRef.value.reset();
		// selectOrder.value = {};
		// selectIndex.value = null;
	}
	async function confirmReplacementImport() {
		// scannedList.value.splice(selectIndex.value, 1)
		scannedList.value[0] = replace.value;
		isReplace.value = false;
		scanBarRef.value.reset();
		uni.showToast({
			title: '替换成功'
		});

	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		flex-direction: column;
	}

	.upload-scan {
		padding: 10px 40rpx 10px 40rpx;
	}

	.area-header {
		color: #909193;
		padding: 10px 15px;
		margin-bottom: 0;
		font-size: 15px;
		background-color: #f3f4f6;
		display: flex;
	}

	.list-content {
		padding: $uni-page-padding;

		.list {
			.card {
				margin-top: 5px;
				margin-bottom: 10px;
				border-radius: 7px;
				display: flex;
				align-items: center;
				padding: 10px 0;
				transition: all 300ms ease;
				box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);

				.left {
					// flex-basis: 90rpx;
					// padding-right: 10px;
					text-align: right;
					font-size: 13px;

					.enter {
						line-height: 20px;
						margin-bottom: 5px;
					}

					.number {
						font-size: 13px;
						line-height: 18px;
					}

					:deep(.u-icon--right) {
						flex-direction: row-reverse;
					}
				}

				.right {
					flex: 1;
					padding-left: 15px;

					.title {
						font-size: 15px;
						line-height: 20px;
						margin-bottom: 5px;
						font-weight: 700;
					}

					.body {
						font-size: 13px;
						line-height: 18px;
						padding-top: 2px;

						.span {
							width: 50%;
							// text-align: left;
							display: inline-block;
						}
					}

					.body1 {
						font-size: 13px;
						line-height: 18px;
						padding-top: 2px;

						.span {
							width: 100%;
							// text-align: left;
							display: inline-block;
						}
					}
				}
			}
		}
	}

	.search {
		display: flex;
		flex: 1;

		.custom-style {
			// width: 120px;
			margin: 12px 5px 5px;
			height: 40px;
			border-radius: 8px;

			&:nth-child(1) {
				flex: 1
			}

			&:nth-child(2) {
				flex: 3
			}
		}
	}
</style>